package com.sailer.practice.guava.Order;


import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.sailer.practice.model.City;
import com.sailer.practice.model.CityByPopulation;
import com.sailer.practice.model.CityByRainfall;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 *
 *Collections.sort(list, new Comparator<Foo>(){
 @Override
 return ComparisonChain.start()
 .compare(f1.a, f2.a, comparatorA)
 .compare(f1.b, f2.b, comparatorB)
 .compare(f1.c, f2.c, comparatorC).result();
 }});
 */
public class OrderStudy {

    public static  void test(){
        Ordering order = Ordering.natural();
        Integer mini = (Integer) order.min(1, 2, 3, 4, 5);
        System.out.println(mini);

        List res = order.greatestOf(Lists.newArrayList(1, 2, 3, 4, 5, 6, 7), 3);
        System.out.println(res);

        CityByRainfall cityByRainfall = new CityByRainfall();

        // 根据第二个参数排序
        City city1 = new City("Beijing", 12, 55.0);
        City city2 = new City("Shanghai", 15, 451.0);
        City city3 = new City("ShenZhen", 9, 33.8);
        City city4 = new City("ShenZhen2", 9, 332.8);

        List<City> cities = Lists.newArrayList(city1, city2, city3,city4);

        System.out.printf("按照降雨量\n");
        /**
         * 单参数排序
         */
        // 降雨量自然排序
        Ordering<City> firstOrdering = Ordering.from(cityByRainfall);
        Collections.sort(cities, firstOrdering);
        Iterator<City> cityByRainfallIterator = cities.iterator();
        while (cityByRainfallIterator.hasNext()) {
            System.out.println(cityByRainfallIterator.next());
        }

        System.out.printf("先按照人口在按照降雨量\n");
        /**
         * 多参数排序
         */
        Ordering<City> secondaryOrdering = Ordering.from(new CityByPopulation()).compound(cityByRainfall);

        Collections.sort(cities, secondaryOrdering);
        Iterator<City> cityIterator = cities.iterator();
        while (cityIterator.hasNext()) {
            System.out.println(cityIterator.next());
        }
        System.out.printf("取得最小最大值\n");
        /**
         * 取得最小最大值
         */
        Ordering<City> ordering = Ordering.from(cityByRainfall);
        // 降雨量最高的2个城市
        List<City> topTwo = ordering.greatestOf(cities, 2);
        Iterator<City> topTwoIterator = topTwo.iterator();
        while (topTwoIterator.hasNext()) {
            System.out.println("降雨量最高城市" + topTwoIterator.next().getCityName());
        }

        System.out.printf("----\n");
        // 降雨量最低的一个城市
        List<City> bottomOne = ordering.leastOf(cities, 1);
        Iterator<City> bottomOneIterator = bottomOne.iterator();
        while (bottomOneIterator.hasNext()) {
            System.out.println("降雨量最低的城市" + bottomOneIterator.next().getCityName());
        }
        System.out.printf("----\n");




    }


    public static void main(String[] args) {
        test();
    }

}
